﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using TheVerge.Phone.Imp;
using Microsoft.Phone.Controls;
using TheVerge.Phone.Controls;

namespace TheVerge.Phone.Pages
{
    public partial class SettingsPage : BasePage
    {
        private Theme ActiveTheme { get { return DataContext as Theme; } }
        private Theme OldTheme;
        private bool Revert = true; //-- By default, revert the change. 
        private bool LeavingBecauseOfAccentColorPicker; //-- List Picker triggers a Navigation. As a result, we have to account for it

        private ThemeBackground SelectedBackground
        {
            get
            {
                return true.Equals(btnDark.IsChecked) ? ThemeBackground.Dark : ThemeBackground.Light;
            }
        }

        public SettingsPage()
        {
            InitializeComponent();

            //-- Add Colors
            var currentColor = Theme.Active.AccentColor;
            bool phoneColorAdded = false;
            foreach (var c in AccentColors.All)
            {
                accentColors.Items.Add(c);
                if (c.Color == currentColor)
                    accentColors.SelectedItem = c;

                if (c.Color == Theme.Phone.AccentColor)
                    phoneColorAdded = true;
            }

            if (!phoneColorAdded)
            {
                var phoneAccentColor = new AccentColor("Phone", Theme.Phone.AccentColor);
                accentColors.Items.Add(phoneAccentColor);
            }

            accentColors.SelectionChanged += AccentColors_SelectionChanged;

            //-- Add Page Orientations
            // System.Enum.GetValues doesn't exist in silverlight for some reason.
            lpOreintations.Items.Add(SupportedPageOrientation.PortraitOrLandscape);
            lpOreintations.Items.Add(SupportedPageOrientation.Portrait);
            lpOreintations.Items.Add(SupportedPageOrientation.Landscape);

            lpOreintations.SelectedItem = Theme.Active.SupportedOrientation;

            if (Theme.Active.Background == ThemeBackground.Light)
                btnLight.IsChecked = true;
            else
                btnDark.IsChecked = true;

            AppBarHelper.ConfirmAction = Confirm;
            AppBarHelper.CancelAction = () => GoBack(true);

            OldTheme = Theme.Active.Copy();
            DataContext = Theme.Active;

            cbUsePhoneTheme.IsChecked = OldTheme.UsePhoneTheme;
        }

        #region Event handlers
        private void AccentColors_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            LeavingBecauseOfAccentColorPicker = true;
        }
        
        private void ChangeBackground_Click(object sender, RoutedEventArgs e)
        {
            ApplyCurrentTheme();
        }

        private void AccentColors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ApplyCurrentTheme();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (LeavingBecauseOfAccentColorPicker)
            {
                LeavingBecauseOfAccentColorPicker = false;
                return;
            }

            if (Revert)
            {
                Theme.SetTheme(OldTheme);
            }
        }

        private void UsePhoneTheme_Click(object sender, RoutedEventArgs e)
        {
            if(cbUsePhoneTheme.IsChecked.HasValue)
                ActiveTheme.UsePhoneTheme = cbUsePhoneTheme.IsChecked.Value;

            ApplyCurrentTheme();
        }
        #endregion

        private void ApplyCurrentTheme()
        {
            if (ActiveTheme.UsePhoneTheme)
            {
                var phoneColor = Theme.Phone.AccentColor;

                foreach (var color in accentColors.Items)
                {
                    var c = color as AccentColor;
                    if (c.Color == phoneColor)
                    {
                        accentColors.SelectedItem = color;
                        break;
                    }
                }

                btnDark.IsChecked = Theme.Phone.Background == ThemeBackground.Dark;
            }

            var selectedColor = accentColors.SelectedItem;

            ActiveTheme.Background = SelectedBackground;
            ActiveTheme.AccentColor = (selectedColor as AccentColor).Color;

            Theme.SetTheme(ActiveTheme);
        }

        private void GoBack(bool revert)
        {
            Revert = revert;

            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
        }

        private void Confirm()
        {
            var color = accentColors.SelectedItem as AccentColor;
            if (color != null)
            {
                ActiveTheme.AccentColor = color.Color;
                ActiveTheme.Background = SelectedBackground;
                ActiveTheme.SupportedOrientation = (SupportedPageOrientation)lpOreintations.SelectedItem;
                Theme.SetTheme(ActiveTheme);
                HeaderControl.NotifyThemeUpdated();
                GoBack(false);
            }
        }

        protected override bool IsMini { get { return false; } }

        protected override IEnumerable<IApplicationBarIconButton> Buttons
        {
            get
            {
                yield return AppBarHelper.ConfirmButton;
                yield return AppBarHelper.CancelButton;
            }
        }

        protected override IEnumerable<IApplicationBarMenuItem> MenuItems { get { return null; } }

        protected override SupportedPageOrientation OverridePageOrientation()
        {
            return SupportedPageOrientation.Portrait;
        }
    }
}